<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="5" :xs="24">
        <div class="head-container">
          <el-tree
            :data="ouTree"
            :expand-on-click-node="false"
            ref="tree"
            node-key="id"
            lazy
            :props="treeProps"
            :load="getSubTree"
            highlight-current
            :default-expanded-keys="expandKeys"
            @current-change="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="19" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="mini"
          :inline="true"
          class="queryForm"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item prop="displayName">
            <el-input
              v-model="queryParams.displayName"
              placeholder="用户账号"
              clearable
              @keyup.enter.native="handleQuery"
            >
              <el-select
                v-model="queryParams.searchScope"
                slot="prepend"
                placeholder="搜索范围"
              >
                <el-option label="全局搜索" value="global"></el-option>
                <el-option
                  label="当前组织"
                  value="current"
                ></el-option> </el-select
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
            <el-button
              size="mini"
              icon="el-icon-download"
              v-hasPermi="['ad:person:importComputer']"
              @click="
                importDialogVisible = true;
                importStep = 0;
              "
              >导入计算机</el-button
            >
            <el-button
              icon="el-icon-upload2"
              size="mini"
              @click="exportPersonExcel"
              >导出</el-button
            >
          </el-form-item>
        </el-form>

        <el-table
          v-loading="loading"
          :data="userList"
          class="dense-table"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            label="用户账号"
            align="left"
            key="samAccountName"
            prop="samAccountName"
          >
            <template scope="scope">
              <el-link type="primary" @click="showInfo(scope.row)">{{
                scope.row.samAccountName
              }}</el-link>
            </template>
          </el-table-column>

          <el-table-column
            label="员工编号"
            align="left"
            key="description"
            prop="description"
          />
          <el-table-column
            label="中文姓名"
            align="left"
            key="displayName"
            prop="displayName"
          />
          <el-table-column label="英文名" align="left" key="name" prop="name" />

          <el-table-column
            label="职务"
            align="left"
            w
            key="jobTitle"
            prop="jobTitle"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="状态" align="center" key="enable">
            <template slot-scope="scope">
              <el-switch
                :value="scope.row.enable"
                :active-value="true"
                :inactive-value="false"
              ></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="邮箱" align="left" key="mail" prop="mail" />
          <el-table-column
            label="职务"
            align="left"
            key="department"
            prop="department"
          />
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 用户详情抽屉 -->
    <el-drawer
      :withHeader="false"
      size="750px"
      v-if="currentPerson"
      :visible.sync="personDetailDrawerVisible"
      direction="rtl"
      class="detail-drawer"
      :modal="false"
      :append-to-body="true"
      :modal-append-to-body="true"
    >
      <div
        style="padding: 20px 15px; max-height: 100vh; overflow: auto"
        v-loading="infoLoading"
      >
        <h3>
          <span> {{ currentPerson.displayName }}</span>
          <!-- <el-divider direction="vertical"></el-divider>
          <span> {{ currentPerson.userPrincipalName }}</span> -->
        </h3>
        <h4 class="info-group-header">基本信息</h4>
        <div class="info-item">
          <span class="info-label">员工编号:</span>
          <span>{{ currentPerson.description }}</span>
        </div>

        <div class="info-item">
          <span class="info-label">用户账号:</span>
          <span>{{ currentPerson.samAccountName }}</span>
        </div>
        <div class="info-item">
          <div class="info-item inline">
            <span class="info-label">中文姓名:</span>
            <span>{{ currentPerson.displayName }}</span>
          </div>
          <div class="info-item inline">
            <span class="info-label">英文名:</span>
            <span>{{ currentPerson.name }}</span>
          </div>
        </div>
        <div class="info-item">
          <span class="info-label">账号（UPN）:</span>
          <span>{{ currentPerson.userPrincipalName }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">用户位置:</span>
          <span>{{ currentPerson.distinguishedName }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">部门:</span>
          <span>{{ currentPerson.department }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">职务:</span>
          <span>{{ currentPerson.jobTitle }}</span>
        </div>
        <div class="info-item">
          <div class="info-label">账号状态:</div>
          <el-switch :value="currentPerson.enable"> </el-switch>
        </div>
        <el-divider></el-divider>
        <h4 class="info-group-header">联系方式</h4>

        <div class="info-item">
          <div class="info-item inline">
            <span class="info-label">手机:</span>
            <span>{{ currentPerson.mobile }}</span>
          </div>
          <div class="info-item inline">
            <span class="info-label">座机:</span>
            <span>{{ currentPerson.telephoneNumber }}</span>
          </div>
        </div>
        <div class="info-item">
          <div class="info-item inline">
            <span class="info-label">邮政编码:</span>
            <span>{{ currentPerson.postalCode }}</span>
          </div>
          <div class="info-item inline">
            <span class="info-label">街道地址:</span>
            <span>{{ currentPerson.postalAddress }}</span>
          </div>
        </div>

        <el-divider></el-divider>
        <h4 class="info-group-header">
          计算机<el-link
            style="float: right"
            v-if="computers && computers.loginType == 'appoint'"
            @click="showManageComputer()"
            >管理<i class="el-icon-arrow-right"></i
          ></el-link>
        </h4>
        <div class="info-item">
          <ul class="my-computer">
            <li>
              <label>默认计算机</label
              ><span
                ><span v-if="computers.default">{{
                  computers.default.name
                }}</span
                ><span v-else>无</span></span
              >
            </li>
            <li>
              <label
                >私人计算机
                <el-tooltip
                  class="item"
                  effect="light"
                  content="用户拥有的计算机，可以自行添加、删除、绑定、解绑"
                  placement="top-start"
                >
                  <i class="el-icon-question" /> </el-tooltip></label
              ><span>{{ computers.personal }}</span>
            </li>
            <li>
              <label
                >公共计算机
                <el-tooltip
                  class="item"
                  effect="light"
                  content="管理员分配给用户的公共计算机，可以解除绑定"
                  placement="top-start"
                >
                  <i class="el-icon-question" />
                </el-tooltip> </label
              ><span>{{ computers.public }}</span>
            </li>
          </ul>
        </div>
      </div>
      <el-form v-if="currentPerson" :model="currentPerson"></el-form>
    </el-drawer>

    <!-- 计算机管理对话框 -->
    <el-dialog
      :visible.sync="manageComputerDialogVisible"
      custom-class="computerDialog"
      title="管理用户计算机"
      width="600px"
    >
      <el-tabs
        type="border-card"
        class="computer-tabs"
        v-model="currentTab"
        @tab-click="getComputers"
        v-loading="fetchingComputers"
      >
        <el-tab-pane label="私人计算机" v-if="personalComputer">
          <div class="tool-bar">
            <div class="toggle">
              <el-tag
                size="mini"
                :type="
                  personalComputer.displayType == 'card' ? 'primary' : 'info'
                "
                @click="personalComputer.displayType = 'card'"
              >
                <svg-icon icon-class="list-toggle"
              /></el-tag>
              <el-tag
                size="mini"
                :type="
                  personalComputer.displayType == 'list' ? 'primary' : 'info'
                "
                style="margin-left: 5px"
                @click="personalComputer.displayType = 'list'"
                ><svg-icon icon-class="list"
              /></el-tag>
            </div>
            <el-input
              size="mini"
              placeholder="计算机账号或名称"
              class="computer-search"
              v-model="personalComputer.searchVal"
            >
              <el-select
                v-model="personalComputer.binded"
                slot="prepend"
                size="mini"
                placeholder="绑定状态"
              >
                <el-option label="全部" value=""></el-option>
                <el-option label="已绑定" :value="true"></el-option>
                <el-option label="未绑定" :value="false"></el-option>
              </el-select>
              <template slot="append"
                ><el-button @click="getComputers" type="primary"
                  >查询</el-button
                ></template
              >
            </el-input>
          </div>
          <template v-if="!personalComputer.list || personalComputer.list == 0">
            <el-empty></el-empty>
          </template>
          <template v-else>
            <el-table
              :data="personalComputer.list"
              v-if="personalComputer.displayType == 'list'"
              size="mini"
            >
              <el-table-column
                label="计算机账号"
                align="left"
                key="samAccountName"
                prop="samAccountName"
              >
              </el-table-column>

              <el-table-column
                label="计算机名称"
                align="left"
                key="name"
                prop="name"
              />
              <el-table-column label="操作" width="100px" align="left">
                <template slot-scope="{ row }">
                  <el-button
                    v-if="!row.binded"
                    size="mini"
                    type="text"
                    @click="bind(row.samAccountName)"
                    >绑定</el-button
                  >
                  <el-button
                    v-if="row.binded && !row.isOld"
                    size="mini"
                    type="text"
                    @click="unbind(row.samAccountName)"
                    >解绑</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <div
              class="card-wrapper"
              style="padding: 0"
              v-if="personalComputer.displayType == 'card'"
            >
              <el-card
                v-for="(item, index) in personalComputer.list"
                :key="index"
                :class="[{ old: item.isOld }, 'computer-card']"
                shadow="hover"
                :body-style="{ padding: '0px' }"
              >
                <div>
                  <div class="computer-icon">
                    <svg-icon
                      :icon-class="item.binded ? 'screen1' : 'screen2'"
                      style="width: 100%; height: 100%"
                    />
                  </div>
                  <div
                    class="tool"
                    style="padding: 10px 5px; text-align: center"
                  >
                    {{ item.name }}
                  </div>
                  <div style="text-align: center">
                    <el-button
                      v-if="!item.binded && !item.isOld"
                      type="text"
                      class="button"
                      size="mini"
                      @click="bind(item.samAccountName)"
                      >绑定</el-button
                    >
                    <el-button
                      v-if="item.binded"
                      type="text"
                      class="button"
                      size="mini"
                      @click="unbind(item.samAccountName)"
                      >解绑</el-button
                    >
                  </div>
                </div>
              </el-card>
            </div>
            <pagination
              layout="total, prev, pager, next"
              v-show="personalComputer.page.total > 0"
              :total="personalComputer.page.total"
              :page.sync="personalComputer.page.pageNum"
              :limit.sync="personalComputer.page.pageSize"
              @pagination="getComputers"
            />
          </template>
        </el-tab-pane>
        <el-tab-pane label="公共计算机" v-if="publicComputer">
          <div class="tool-bar">
            <div class="toggle">
              <el-tag
                size="mini"
                :type="
                  publicComputer.displayType == 'card' ? 'primary' : 'info'
                "
                @click="publicComputer.displayType = 'card'"
              >
                <svg-icon icon-class="list-toggle"
              /></el-tag>
              <el-tag
                size="mini"
                :type="
                  publicComputer.displayType == 'list' ? 'primary' : 'info'
                "
                style="margin-left: 5px"
                @click="publicComputer.displayType = 'list'"
                ><svg-icon icon-class="list"
              /></el-tag>
            </div>
            <el-input
              size="mini"
              class="computer-search"
              placeholder="计算机账号或名称"
              v-model="publicComputer.searchVal"
            >
              <el-select
                v-model="publicComputer.binded"
                slot="prepend"
                placeholder="绑定状态"
              >
                <el-option label="全部" value=""></el-option>
                <el-option label="已绑定" :value="true"></el-option>
                <el-option label="未绑定" :value="false"></el-option>
              </el-select>
              <template slot="append"
                ><el-button @click="getComputers" type="primary"
                  >查询</el-button
                ></template
              >
            </el-input>
          </div>
          <template v-if="!publicComputer.list || publicComputer.list == 0">
            <el-empty></el-empty>
          </template>
          <template v-else>
            <el-table
              :data="publicComputer.list"
              v-if="publicComputer.displayType == 'list'"
              size="mini"
            >
              <el-table-column
                label="计算机账号"
                align="left"
                key="samAccountName"
                prop="samAccountName"
              >
                <template scope="scope">
                  {{ scope.row.samAccountName }}
                  <!-- <el-link type="primary" @click="showInfo(scope.row)">{{
                  scope.row.cn
                }}</el-link> -->
                </template>
              </el-table-column>

              <el-table-column
                label="计算机名称"
                align="left"
                key="name"
                prop="name"
              />
              <el-table-column label="操作" align="left" width="100px">
                <template slot-scope="{ row }">
                  <el-button
                    v-if="!row.binded"
                    size="mini"
                    type="text"
                    @click="bind(row.samAccountName)"
                    >绑定</el-button
                  >
                  <el-button
                    v-if="row.binded"
                    size="mini"
                    type="text"
                    @click="unbind(row.samAccountName)"
                    >解绑</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <div
              class="card-wrapper"
              style="padding: 0"
              v-if="publicComputer.displayType == 'card'"
            >
              <el-card
                v-for="(item, index) in publicComputer.list"
                :key="index"
                class="computer-card"
                shadow="hover"
                :body-style="{ padding: '0px' }"
              >
                <div>
                  <div class="computer-icon">
                    <svg-icon
                      :icon-class="item.binded ? 'screen1' : 'screen2'"
                      style="width: 100%; height: 100%"
                    />
                  </div>
                  <div
                    class="tool"
                    style="padding: 10px 5px; text-align: center"
                  >
                    {{ item.name }}
                  </div>
                  <div style="text-align: center">
                    <el-button
                      v-if="!item.binded && !item.isOld"
                      type="text"
                      class="button"
                      size="mini"
                      @click="bind(item.samAccountName)"
                      >绑定</el-button
                    >
                    <el-button
                      v-if="item.binded"
                      type="text"
                      class="button"
                      size="mini"
                      @click="unbind(item.samAccountName)"
                      >解绑</el-button
                    >
                  </div>
                </div>
              </el-card>
            </div>
            <pagination
              layout="total, prev, pager, next"
              v-show="publicComputer.page.total > 0"
              :total="publicComputer.page.total"
              :page.sync="publicComputer.page.pageNum"
              :limit.sync="publicComputer.page.pageSize"
              @pagination="getComputers"
            />
          </template>
        </el-tab-pane>
        <!-- <el-tab-pane label="批量导入">
          <el-form label-width="200px" v-model="importForm">
            <el-form-item label="导入方式" key="importType" prop="importType">
              <el-radio v-model="importForm.importType" label="upload"
                >上传EXCEL
              </el-radio>

              <el-radio v-model="importForm.importType" label="clipboard"
                >粘贴文本</el-radio
              >
            </el-form-item>
            <div
              style="padding:5px 20px;"
              v-if="importForm.importType == 'clipboard'"
            >
              <el-alert
                title="文本格式"
                type="info"
                description="用户账号 （邮件地址）|计算机账号1，计算机账号2，计算机账号3;"
                show-icon
              >
              </el-alert>

              <el-input
                type="textarea"
                :rows="10"
                placeholder="zhangsan1|Computer-01,Computer-01,Computer-01
zhangsan2|Computer-03,Computer-04,Computer-05"
                v-model="importForm.clipboardText"
              >
              </el-input>
            </div>
          </el-form>

          <el-upload
            v-if="importForm.importType == 'upload'"
            style="text-align:center"
            class="upload-file"
            drag
            action="https://jsonplaceholder.typicode.com/posts/"
            multiple
            :before-upload="beforeUpload"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
            <div
              class="el-upload__tip"
              slot="tip"
              style="display: flex;justify-content: center;align-items: center;"
            >
              请按<el-link
                href="http://localhost:12345/api/ad/person/downloadPersonTemplate"
                type="primary"
                >模板</el-link
              >填写数据后，上传Excel文件
            </div>
          </el-upload>

          <div style="text-align:center;margin-top:20px;">
            <el-button
              v-if="importForm.importType == 'upload'"
              type="primary"
              @click="submitUpload()"
              >开始导入</el-button
            >
            <el-button
              v-if="importForm.importType == 'clipboard'"
              type="primary"
              @click="confirmImport()"
              >开始导入</el-button
            >
          </div>
        </el-tab-pane> -->
      </el-tabs>
    </el-dialog>

    <!-- 导入弹窗 -->
    <el-dialog
      :visible.sync="importDialogVisible"
      custom-class="computerDialog"
      title="批量导入"
      width="600px"
    >
      <div style="width: 400px; margin: 0 auto">
        <el-steps :active="importStep" simple>
          <el-step title="导入数据" icon="el-icon-edit"></el-step>
          <el-step title="查看结果" icon="el-icon-upload"></el-step>
        </el-steps>
      </div>
      <template v-if="importStep == 0">
        <el-form label-width="200px" v-model="importForm">
          <el-form-item label="导入方式" key="importType" prop="importType">
            <el-radio v-model="importForm.importType" label="upload"
              >上传EXCEL
            </el-radio>

            <el-radio v-model="importForm.importType" label="clipboard"
              >粘贴文本</el-radio
            >
          </el-form-item>
          <div
            style="padding: 5px 20px"
            v-if="importForm.importType == 'clipboard'"
          >
            <el-alert
              title="文本格式"
              type="info"
              description="用户账号 （邮件地址）|计算机账号1，计算机账号2，计算机账号3;"
              show-icon
            >
            </el-alert>

            <el-input
              type="textarea"
              :rows="10"
              placeholder="zhangsan1|Computer-01,Computer-01,Computer-01
zhangsan2|Computer-03,Computer-04,Computer-05"
              v-model="importForm.clipboardText"
            >
            </el-input>
          </div>
        </el-form>
        <el-upload
          v-if="importForm.importType == 'upload'"
          style="text-align: center"
          class="upload-file"
          drag
          action="https://jsonplaceholder.typicode.com/posts/"
          multiple
          :before-upload="beforeUpload"
          :limit="1"
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">
            只能上传xls/xlsx文件，且不超过10M
          </div>
          <div
            class="el-upload__tip"
            slot="tip"
            style="display: flex; justify-content: center; align-items: center"
          >
            >填写数据后，上传Excel文件 请按<el-link
              :href="`${env.VUE_APP_BASE_API}/ad/person/downloadPersonTemplate`"
              type="primary"
              >模板</el-link
            >填写数据后，上传Excel文件
          </div>
        </el-upload>

        <div style="text-align: center; margin-top: 20px">
          <el-button
            v-if="importForm.importType == 'upload'"
            type="primary"
            @click="submitUpload()"
            >开始导入</el-button
          >
          <el-button
            v-if="importForm.importType == 'clipboard'"
            type="primary"
            @click="confirmImport()"
            >开始导入</el-button
          >
        </div>
      </template>
      <div v-if="importStep == 1" style="padding-top: 10px">
        <el-table :data="ImportStateList" size="mini" style="width: 100%">
          <el-table-column
            label="账号"
            align="left"
            key="samAccountName"
            prop="samAccountName"
          />
          <el-table-column
            label="计算机"
            align="left"
            key="userWorkstations"
            prop="userWorkstations"
          />
          <el-table-column
            label="导入状态"
            align="left"
            key="importState"
            prop="importState"
          >
            <template scope="{row}"
              ><span
                v-if="row.importState == '成功'"
                style="color: green"
                class="el-icon-success"
                >成功</span
              >
              <span
                v-if="row.importState == '失败'"
                style="color: red"
                class="el-icon-error"
                >失败</span
              >
            </template>
          </el-table-column>

          <el-table-column
            label="错误详情"
            show-overflow-tooltip
            align="left"
            key="errorMsg"
            prop="errorMsg"
          />
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ouApi from "@/api/activeDirectory/ou";
import personApi from "@/api/activeDirectory/person";
import { orgMixin } from "../mixin/org.js";

export default {
  name: "person",
  mixins: [orgMixin],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",

      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      // 性别状态字典
      sexOptions: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      //上传返回的数据
      ImportStateList: null,
      // 表单参数
      form: {},
      currentPerson: undefined,
      computers: {},
      personalComputer: {
        list: [],
        page: { pageNum: 1, pageSize: 9, total: 0 },
        displayType: "card",
        binded: "",
        searchVal: "",
      },
      publicComputer: {
        list: [],
        page: { pageNum: 1, pageSize: 9, total: 0 },
        displayType: "card",
        searchVal: "",
        binded: "",
      },
      importForm: {
        importType: "upload", //导入数据方式 upload 或 clipboard

        clipboardText: "",
      },
      fetchingComputers: false,
      manageComputerDialogVisible: false,
      currentTab: 0, //当前打开计算机管理弹窗的tab页面
      personDetailDrawerVisible: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        displayName: undefined,
        deptId: undefined,
        cn: undefined,
        searchScope: "current",
        distinguishedName: undefined,
      },
      importDialogVisible: false,
      importStep: 0,
      infoLoading: false,
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  computed: {
    env() {
      return process.env;
    },
  },
  mounted() {},

  methods: {
    /** 查询用户列表 */
    getList() {
      if (
        this.queryParams.searchScope == "global" &&
        !this.queryParams.displayName
      ) {
        this.$message.warning("全局搜索必须输入查询条件！");
        return;
      }
      this.loading = true;
      personApi
        .pageList(this.queryParams)
        .then((res) => {
          this.userList = res.data.data;
          this.total = res.data.pagination.dataCount;
        })
        .catch()
        .finally((n) => {
          this.loading = false;
        });
    },
    // 导出
    exportPersonExcel() {
      this.loading = true;
      personApi
        .exportExcel(this.queryParams)
        .then((res) => {
          const content = res.data;
          const blob = new Blob([content], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8",
          });
          var headerVal = res.headers["content-disposition"].split(";")[1];
          const fileName = headerVal.substr("filename=".length + 1);
          if (navigator.msSaveBlob) {
            navigator.msSaveBlob(blob, fileName);
          } else {
            let url = window.URL.createObjectURL(blob);
            const link = document.createElement("a");
            link.href = url;
            link.download = fileName;
            link.click();
            url.revokeObjectURL(link.href);
          }
        })
        .catch()
        .finally((n) => {
          this.loading = false;
        });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(node) {
      this.queryParams.cn = node.data.cn;
      this.queryParams.distinguishedName = node.data.distinguishedName;
      this.queryParams.searchScope = "current";
      this.getList();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: [],
      };
      this.resetForm("form");
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    //查看详细信息
    async showInfo(row) {
      this.personDetailDrawerVisible = true;
      this.infoLoading = true;
      personApi
        .computers(row.samAccountName)
        .then((res) => {
          if (res.success) {
            this.computers = res.data;
          }
        })
        .catch((n) => {})
        .finally((n) => {
          this.infoLoading = false;
        });

      const res = await personApi.info(row.samAccountName);
      if (res.success) {
        this.currentPerson = res.data;
      }
    },
    async showManageComputer() {
      this.manageComputerDialogVisible = true;
      this.getComputers();
    },
    async getComputers() {
      const { samAccountName } = this.currentPerson;
      this.fetchingComputers = true;
      if (this.currentTab == 0) {
        personApi
          .personalComputer(samAccountName, {
            ...this.personalComputer.page,
            searchVal: this.personalComputer.searchVal,
            binded: this.personalComputer.binded,
          })
          .then((res) => {
            if (res.success) {
              this.personalComputer.list = res.data.data;
              this.personalComputer.page.total = res.data.pagination.dataCount;
            }
          })
          .finally((n) => {
            this.fetchingComputers = false;
          });
      } else if (this.currentTab == 1) {
        personApi
          .publicComputer(samAccountName, {
            ...this.publicComputer.page,
            searchVal: this.publicComputer.searchVal,
            binded: this.publicComputer.binded,
          })
          .then((res) => {
            if (res.success) {
              this.publicComputer.list = res.data.data;
              this.publicComputer.page.total = res.data.pagination.dataCount;
            }
          })
          .finally((n) => {
            this.fetchingComputers = false;
          });
      }
    },

    //绑定计算机
    async bind(computerAccount) {
      const { samAccountName } = this.currentPerson;
      var res = undefined;
      if (this.currentTab == 0) {
        res = await personApi.bindPersonalComputer(
          samAccountName,
          computerAccount
        );
      } else if (this.currentTab == 1) {
        res = await personApi.bindPersonalComputer(
          samAccountName,
          computerAccount
        );
      }
      if (res.success) {
        this.$message.success("绑定成功!");
        this.getComputers();
      }
    },
    //解绑计算机
    async unbind(computerAccount) {
      const { samAccountName } = this.currentPerson;
      var type = "personal";

      if (this.currentTab == 1) {
        type = "public";
      }
      const res = await personApi.unBindPublicComputer(
        samAccountName,
        computerAccount,
        type
      );
      if (res.success) {
        this.$message.success("解绑成功!");
        this.getComputers();
      }
    },
    beforeUpload(file) {
      this.files = file;
      const extension = file.name.split(".")[1] === "xls";
      const extension2 = file.name.split(".")[1] === "xlsx";
      const isLt5M = file.size / 1024 / 1024 < 10;
      if (!extension && !extension2) {
        this.$message.warning("上传模板只能是 xls、xlsx格式!");
        return;
      }
      if (!isLt5M) {
        this.$message.warning("上传模板大小不能超过 10MB!");
        return;
      }
      this.fileName = file.name;
      return true; // 返回false不会自动上传
    },
    // 上传excel
    submitUpload() {
      console.log("上传" + this.files.name);
      if (this.fileName == "") {
        this.$message.warning("请选择要上传的文件！");
        return false;
      }
      let fileFormData = new FormData();
      //filename是键，file是值，就是要传的文件，test是要传的文件名
      fileFormData.append("files", this.files, this.fileName);
      let requestConfig = {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      };
      // 执行上传excel
      personApi
        .importExcel(fileFormData)
        .then((resp) => {
          console.log(resp);
          if (resp.success) {
            this.importStep = 1;
            this.ImportStateList = resp.data;
          }
        })
        .catch((e) => {
          console.log(e);
          // this.$message.error("excel上传失败，请重新上传");
        });
    },

    //剪贴板
    async confirmImport() {
      if (!this.importForm.clipboardText) {
        this.$message.warning("复制文本不能为空");
        return;
      }
      const res = await personApi.importComputerFromClipboard({
        value: this.importForm.clipboardText,
      });
      if (res.success) {
        this.importStep = 1;
        this.ImportStateList = res.data;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../computer/style/computerCard.scss";
.my-computer {
  display: flex;
  justify-content: space-between;
  margin: 0;
  padding: 0;
  > li {
    list-style: none;
    width: 33%;
    border-right: 1px solid #e6e9e9;
    margin: 10px;
    > label {
      display: block;
      color: #555 !important;
      margin-bottom: 10px;
      font-size: 14px;
    }
    > span {
      font-size: 24px;
      color: #ff7a4b;
      margin-right: 10px;
      vertical-align: baseline;
    }
  }
}
.tool-bar {
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .toggle {
    cursor: pointer;
    flex-basis: 70%;
  }
}
</style>
<style lang="scss">
.computerDialog {
  .el-dialog__body {
    padding: 5px 20px 20px 20px;
  }
}
.computer-search .el-select .el-input {
  width: 85px;
  input {
    text-align: center;
  }
}
</style>
